package org.ruiyichen.guizhou.agr.ui.mineview;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.TextView;

import org.ruiyichen.guizhou.agr.R;
import org.ruiyichen.guizhou.agr.ui.mineview.number_progressbar.NumberProgressBar;


public class NumberHProgressDialog extends Dialog {

    public enum TYPE {
        FORCE/*强制*/, NO_COERCION/*不强制*/
    }

    private TYPE mType = TYPE.FORCE;

    private Context context = null;
    private static NumberHProgressDialog customProgressDialog = null;

    private NumberProgressBar mNumberHProgressDialog;

    private TextView mTextView;

    private CustomDialog customDialog;

    public NumberHProgressDialog(Context context) {
        super(context);
        this.context = context;
    }

    public NumberHProgressDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    public static NumberHProgressDialog createDialog(Context context) {
        customProgressDialog = new NumberHProgressDialog(context, R.style.CustomProgressDialog);
        customProgressDialog.setContentView(R.layout.dialog_h_progress);
        customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
//        customProgressDialog.setOnKeyListener(customProgressDialog);
        return customProgressDialog;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    public void onWindowFocusChanged(boolean hasFocus) {
        if (customProgressDialog == null) {
            return;
        }
        if (mNumberHProgressDialog == null)
            mNumberHProgressDialog = (NumberProgressBar) customProgressDialog.findViewById(R.id.npb_progress_bar);
        if (mTextView == null)
            mTextView = (TextView) findViewById(R.id.tv_loading_msg);
    }

    /**
     * [Summary]
     * setTitile 标题
     *
     * @param strTitle
     * @return
     */
    public NumberHProgressDialog setTitile(String strTitle) {
        return customProgressDialog;
    }

    /**
     * [Summary]
     * setMessage 提示内容
     *
     * @param strMessage
     * @return
     */
    public NumberHProgressDialog setMessage(String strMessage) {
        if (mTextView != null) {
        } else {
            mTextView = (TextView) customProgressDialog.findViewById(R.id.tv_loading_msg);
        }
        mTextView.setText(strMessage);
        return customProgressDialog;
    }

    public void setProgressDialog(int progress) {
        if (mNumberHProgressDialog != null) {
            mNumberHProgressDialog.setProgress(progress);
        }
    }

    public NumberHProgressDialog setType(TYPE mType) {
        this.mType = mType;
        return customProgressDialog;
    }

    public NumberHProgressDialog setCustomDialog(CustomDialog customDialog) {
        this.customDialog = customDialog;
        return customProgressDialog;
    }

    @Override
    public void onBackPressed() {
        if (mType == TYPE.FORCE) {
            return;
        }
        if (customDialog == null) {
            super.onBackPressed();
            return;
        }
        customDialog.show();
    }
}